<style>
    .layui-form-item .layui-inline {
        margin-top: 10px;
    }
    .layui-form-label{
        width: 100px;
    }
</style>
<div class="layui-fluid" id="Inventory-edit">
    <div class="layui-card">
        <div class="layui-form" lay-filter="LAY-list-form">
            <div id="view"></div>
        </div>
    </div>
</div>
<script id="page1" type="text/html">
    <input type="hidden" id="id" name="id" value="{{layui.router().search.id||''}}">
    <input type="hidden" id="type" name="type" value="{{layui.router().search.type||''}}">
    <input type="hidden" id="ware_type" name="ware_type" value="{{layui.router().search.ware_type||''}}">
    <div class="layui-card-header">盘点单资料</div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">盘点单号：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none">{{d.inventory_sn||''}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">仓库：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none">{{d.warehouse_name||''}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">盘点类型：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none">{{d.type_name||''}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">创建时间：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none">{{d.create_time||''}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">完成时间：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none">{{d.complete_time||''}}</span>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">盘点人：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none">{{d.operator||''}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">总差异：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none" id="difference_quantity_all">{{d.difference_quantity_all}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">总差异金额：</label>
            <div class="layui-input-inline layui-jy-width200">
                <span class="layui-input layui-input-none" id="difference_money_all">{{d.difference_money_all}}</span>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">审核状态：</label>
            <div class="layui-input-inline">
                <span class="layui-input layui-input-none">{{d.status_name||''}}</span>
            </div>
        </div>
    </div>

    <div class="layui-card-header">商品信息</div>
    <div class="layui-card-body">
        <div class="layui-tab">
            <ul class="layui-tab-title">
                <li class="layui-this">明细信息</li>
            </ul>
            <div class="layui-tab-content">
                <div class="tab1 layui-tab-item layui-show">
                    <div class="layui-item">
                        <!--{{# if(!layui.router().search.id) { }}-->
                        <!--<button class="uploadPage layui-btn">导入</button>-->
                        <!--<button class="add layui-btn">新增</button>-->
                        <!--{{#} }}-->

                        {{# if(layui.router().search.type == 2){ }}
                        <button class="export1 layui-btn">导出</button>
                        {{# } }}
                        {{# if(layui.router().search.type == 3){ }}
                        <button class="uploadPage layui-btn">导入盘点结果</button>
                        {{# } }}

                    </div>
                    <table id="LAY-list" lay-filter="LAY-list"></table>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card-body layui-jy-center">
        <a lay-href="Inventory/getList" class="layui-btn">返回</a>
        {{# if(layui.router().search.type == 1){ }}
        <input type="button"  lay-submit lay-filter="LAY-submit-1" value="通过" class="layui-btn layui-btn-normal LAY-submit1">
        <input type="button"  lay-submit lay-filter="LAY-submit-2" value="不通过" class="layui-btn layui-btn-danger LAY-submit1">
        {{# }else if(layui.router().search.type == 3){ }}
        <input type="button"  lay-submit lay-filter="LAY-submit-3" value="保存" class="layui-btn layui-btn-normal LAY-submit1">
        <input type="button"  lay-submit lay-filter="LAY-submit-4" value="提审" class="layui-btn layui-btn-danger LAY-submit1">
        {{# } }}
    </div>

</script>
<!-- 行内工具 -->
<script type="text/html" id="LAY-list-toolbar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del" href="javascript:;">删除</a>
</script>
<script>
    var ID = "";
    layui.use(['admin', 'form', 'toolJ', 'laytpl', 'laydate', 'upload', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , laytpl = layui.laytpl
            , setter = layui.setter
            , laydate = layui.laydate
            , table = layui.table
            , toolJ = layui.toolJ;
        var id = layui.router().search.id;
        ID = id;
        var type = layui.router().search.type;
        function tableView(data) {
            var url = '';
            var jdata = [];
            if (data.length > 0) {
                jdata = data;
            }
            table.render({
                elem: '#LAY-list'
                , url: url
                , data: jdata
                , toolbar: true
                , defaultToolbar: ['exports', 'filter', 'print']
                , cellMinWidth: 80
                , cols: [[
                    {field: 'sku', title: 'sku'}
                    , {field: 'sku_name', title: 'SKU中文名称'}
                    , {field: 'ean', title: 'EAN'}
                    , {field: 'stock_quantity', title: '库存数量'}
                    , {field: 'unit_price', title: '单价'}
                    , {field: 'inventory_quantity', title: '盘点数量*', edit:"text"}
                    , {field: 'difference_quantity', title: '盘点差异*'}
                ]]
                , page: false
                , limit: 10000
                , text: {none: '暂无相关数据'}
            });
        }

        function viewHtml(data) {
            var getTpl = page1.innerHTML
                , view = $('#view');
            laytpl(getTpl).render((data.info)[0], function (html) {
                view.html(html);
            });
            tableView(data.detail);
            table.render(null, 'LAY-list');
            form.render(null, 'LAY-list-form');
        }
        if (id) {
            toolJ.ajax({"data": {"id": id}, "type": "get"},
                function (data) {
                    viewHtml({"info": data.data.info, "detail": data.data.detail});
                }
            );
        }
        //监听通过
        form.on('submit(LAY-submit-1)', function (data) {
            if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                data.field['type'] = type;
                data.field['status'] = 30;
                data.field.list = JSON.stringify(table.cache['LAY-list']);
                toolJ.ajax({"data": data.field},function (data) {
                    layer.alert(data.msg);
                    if (data.code == 1){
                        window.location.href = "/#/Inventory/getList";
                    }
                });
            } else {
                toolJ.error('不存在明细!');
            }
        });

        //监听不通过
        form.on('submit(LAY-submit-2)', function (data) {
            if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                data.field['type'] = type;
                data.field['status'] = 40;
                data.field.list = JSON.stringify(table.cache['LAY-list']);
                toolJ.ajax({"data": data.field},function (data) {
                    layer.alert(data.msg);
                    if (data.code == 1){
                        window.location.href = "/#/Inventory/getList";
                    }
                });
            } else {
                toolJ.error('不存在明细!');
            }
        });

        //监听保存
        form.on('submit(LAY-submit-3)', function (data) {
            if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                var difference_quantity_all_obj = $('#difference_quantity_all');
                var difference_money_all_obj = $('#difference_money_all');
                data.field['difference_quantity_all'] = difference_quantity_all_obj.html();
                data.field['difference_money_all'] = difference_money_all_obj.html();
                data.field.list = JSON.stringify(table.cache['LAY-list']);
                var flag = true;
                $.each(table.cache['LAY-list'],function (index,item) {
                    if (item.inventory_quantity == ""){
                        layer.alert("第"+(index+1)+"行盘点数量不能为空");
                        flag = false;
                        return false;
                    }
                });
                if (flag){
                    toolJ.ajax({"url":"index/Inventory/saveData","data": data.field},function (data) {
                        layer.alert(data.msg);
                        if (data.code == 1){
                            window.location.href = "/#/Inventory/getList";
                        }
                    });
                }

            } else {
                toolJ.error('不存在明细!');
            }
        });

        //监听提审
        form.on('submit(LAY-submit-4)', function (data) {
            if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                var difference_quantity_all_obj = $('#difference_quantity_all');
                var difference_money_all_obj = $('#difference_money_all');
                data.field['difference_quantity_all'] = difference_quantity_all_obj.html();
                data.field['difference_money_all'] = difference_money_all_obj.html();
                data.field['is_tishen'] = true; // 提审
                data.field.list = JSON.stringify(table.cache['LAY-list']);
                var flag = true;
                $.each(table.cache['LAY-list'],function (index,item) {
                    if (item.inventory_quantity == ""){
                        layer.alert("第"+(index+1)+"行盘点数量不能为空");
                        flag = false;
                        return false;
                    }
                });
                if (flag){
                    toolJ.ajax({"url":"index/Inventory/saveData","data": data.field},function (data) {
                        layer.alert(data.msg);
                        if (data.code == 1){
                            window.location.href = "/#/Inventory/getList";
                        }
                    });
                }

            } else {
                toolJ.error('不存在明细!');
            }
        });


        /*导出*/
        $("body").off('click.export1').on('click.export1', '.export1', function () {
            $(".layui-inline[lay-event='LAYTABLE_EXPORT']").click();
            $(".tab1 li[data-type='csv']").click();
        });


        /*监听上传文件*/
        $('#Inventory-edit').off('click.uploadPage').on('click.uploadPage', '.uploadPage', function () {
            var url = "/index/Inventory/upload?id="+ID;
            var csvUrl = "/template/importcvs/inventory.csv";
            toolJ.upload(url,csvUrl,function (res) {
                if (res.code == 1){
                    layer.msg(res.msg);
                    var difference_quantity_all_obj = $('#difference_quantity_all');
                    var difference_money_all_obj = $('#difference_money_all');
                    difference_quantity_all_obj.html(res.info.difference_quantity_all);
                    difference_money_all_obj.html(res.info.difference_money_all);
                    tableView(res.data);
                    table.render(null, 'LAY-list');
                }else {
                    layer.alert(res.msg,{icon: icon});
                }
            });
        });

        /*监听编辑数据*/
        table.on('edit(LAY-list)', function (obj) {
            if (type != 3){
                setTimeout(function () {
                    obj.update({inventory_quantity: obj.data.original_inventory_quantity});
                }, 100);
                return false;
            }
            var that = this;
            var tdElem = $(that).closest('td');
            var field = obj.field;
            var value = obj.value;
            if (field === 'inventory_quantity') {
                setTimeout(function () {
                    var reg=/^([1-9]\d*|[0]{1,1})$/; //含0正整数
                    if(!reg.test(value)){
                        layer.msg('盘点数量填写有误!', {anim: 6});
                        obj.update({inventory_quantity: obj.data.original_inventory_quantity});
                        tdElem.click();
                        return false;
                    }else {
                        var difference_quantity_all_obj = $('#difference_quantity_all');
                        var difference_money_all_obj = $('#difference_money_all');
                        var quantity = 0;
                        var money = 0;
                        $.each(table.cache['LAY-list'],function (index,item) {
                            if (item.inventory_quantity != "") {
                                quantity += parseInt(item.inventory_quantity) - parseInt(item.stock_quantity);
                                money += (parseInt(item.inventory_quantity) - parseInt(item.stock_quantity)) * item.unit_price;
                            }
                        });
                        difference_quantity_all_obj.html(quantity);
                        difference_money_all_obj.html(money);
                        obj.update({difference_quantity: parseInt((obj.data.inventory_quantity - obj.data.stock_quantity))});
                    }
                }, 100);
            }
        });

    });

</script>
